package com.icoolkj.learn.juc.base;

import lombok.extern.slf4j.Slf4j;

/**
 * @author icoolkj
 * @version 1.0
 * @description 线程 join 方法
 * @createDate 2025/03/04 12:43
 */
@Slf4j
public class ThreadJoinDemo {

    private static int count = 0;

    public static void main(String[] args) {
        log.info("{} 开始执行...", Thread.currentThread().getName() );

        Runnable runnable = () ->{
            log.info("{} 开始执行", Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            count = 5;
            log.info("{} 执行完成", Thread.currentThread().getName());
        };

        Thread thread1 = new Thread(runnable, "t1");
        thread1.start();

        try {
            thread1.join();  // 让 thread 直线完成后再继续执行
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        log.info("结果为 {}", count);
        log.info("{} 执行完成", Thread.currentThread().getName());
    }

}
